<?php

class CategoryController extends \AdminController {
    protected $layout = 'admin::layouts.master';
    /**
     * Display a listing of the resource.
     * GET /category
     *
     * @return Response
     */
    public function index()
    {
        $dataPost = array(
            'keySearch' => ''
        );

        if (Request::isMethod('post')) {
            $dataPost['keySearch'] = Input::get('keySearch');
        }

        $categories = Category::getAll($dataPost);

        return View::make('admin::category.index', array('categories' => $categories, 'data' => $dataPost))->with('title', 'List category');
    }

    /**
     * Show the form for creating a new resource.
     * GET /category/create
     *
     * @return Response
     */
    public function create()
    {
        $category = new Category();

        $data = array('category' => $category,);

        return View::make('admin::category.create', $data)->with('title', 'Create category');
    }

    /**
     * Store a newly created resource in storage.
     * POST /category
     *
     * @return Response
     */
    public function store()
    {
        if (Request::isMethod('post')) {
            $category = category::find(Input::get('id'));

            $rules = array(
                'category_name'         => 'required|max:50',
                'category_description'  => 'required|max:300',
            );

            $validator = Validator::make(Input::all(), $rules);

            if ($validator->fails()) {
                return Redirect::to($this->packageAdmin.'/admin/category/create')->withErrors($validator)->withInput()->with('title', 'Create category');
            } else {
                if (!$category){
                    $category = new Category();
                }

                $category->category_name   = Input::get('category_name');
                $category->category_description  = Input::get('category_description');
                $category->status    = Input::get('status');

                DB::beginTransaction();
                if ($category->save()) {
                    if ($category->id &&  $category->update()) {
                        DB::commit();

                        return Redirect::to($this->packageAdmin.'/admin/category')->with('success', 'Category saved in successful.');
                    }
                } else {
                    DB::rollback();
                }
            }
        }

        $category = new Category();

        $data = array('category' => $category);

        return View::make('admin::category.create', $data)->with('title', 'Create Category');
    }

    /**
     * Display the specified resource.
     * GET /category/{id}
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id)
    {
        if ($id) {
            $category = Category::findOrFail($id);

            if ($category) {
                return View::make('admin::category.view', array('category' => $category))->with('title', $category->category_name);
            } else {
                return Redirect::to('dtu/admin/category')->with('flash_error', 'Category id ='. $id. ' not found.');
            }
        }

        return Redirect::to('dtu/admin/category');
    }

    /**
     * Show the form for editing the specified resource.
     * GET /category/{id}/edit
     *
     * @param  int  $id
     * @return Response
     */
    public function edit($id)
    {
        if ($id) {
            $category = Category::findOrFail($id);

            if ($category) {
                $data = array('category' => $category,);

                return View::make('admin::category.create', $data)->with('title', $category->category_name);
            } else {

                return Redirect::to('dtu/admin/category')->with('flash_error', 'Category id '. $id. ' not found.');
            }
        }

        return Redirect::to('/dtu/admin/category');
    }

    /**
     * Remove the specified resource from storage.
     * DELETE /category/{id}
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy($id)
    {
        $category = Category::findOrFail($id);

        if ($category) {
            if ($category->delete_flag == 0) {
                $category->delete_flag = 1;
            } else {
                $category->delete_flag = 0;
            }

            if ($category->update(array('delete_flag'))) {
                return Redirect::route('category')->with('flash_error', 'Category id '. $id. ' deleted.');
            }
        }

        return Redirect::route('category')->with('flash_error', 'Category id '. $id. ' not found.');
    }

    public function updateStatus()
    {
        $data = array(
            'error'  => true,
            'status' => 0
        );

        if (Request::isMethod('get') && $id = Request::get('id')) {

            $category = Category::findOrFail($id);

            if ($category) {
                if ($category->status == 0) {
                    $category->status = 1;
                    $data['status'] = 1;
                } else {
                    $category->status = 0;
                    $data['status'] = 0;
                }

                if ($category->update(array('status'))) {
                    $data['error'] = false;
                }
            }
        }


        return Response::json($data);
    }

}